package cn.itcast.config;

import cn.itcast.handler.CustomAccessDecisionManager;
import cn.itcast.handler.CustomFilterInvocationSecurityMetadataSource;
import cn.itcast.handler.HandleControllerException;
import cn.itcast.handler.MyUserDetailService;
import cn.itcast.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;





/**
 * 配置类
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private HandleControllerException handleControllerException;

    @Autowired
    private CustomFilterInvocationSecurityMetadataSource customFilterInvocationSecurityMetadataSource;

    @Autowired
    private CustomAccessDecisionManager customAccessDecisionManager;

    @Autowired
    private MyUserDetailService myUserDetailService;

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    //配置用户和角色
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailService);
    }


    //配置角色和权限
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()//http请求认证
                //放行资源
                .antMatchers("/css/**","/img/**","/js/**","/plugins/**","/login.html").permitAll()
                .antMatchers("/index.html")//设置权限路径
                .fullyAuthenticated()//设置只要登录的用户都可以访问
                //.hasAnyRole("ADMIN","USER","HEALTH_MANAGER")//设置可以访问该请求的角色
                .anyRequest().authenticated()//所有的请求都要经过认证才可以通过
                .and()
                .formLogin()//而且支持基于from表单的登录,默认登录页面
                .usernameParameter("username")
                .passwordParameter("password")
                .loginPage("/login.html")//设置自定义登录的控制器路径，该地址要和表单提交的地址一致
                .loginProcessingUrl("/user/login")
                .permitAll()
                .defaultSuccessUrl("/index.html")
                //退出功能，
                .and().logout().logoutSuccessUrl("/login.html").logoutUrl("/user/logout")
                .and()
                .httpBasic()//HTTPBasic方式的认证
                .and()
                .csrf().disable()//关闭跨域伪造，否则表单提交不成功
                .exceptionHandling().accessDeniedHandler(handleControllerException)//设置自定义403异常
                .and()
                //.headers().frameOptions().disable();//解决springSecurity和ifram框架不展示问题
                .headers().frameOptions().sameOrigin();//解决springSecurity和ifram框架不展示问题
    }
}